﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Utility;
using Module.BusinessLogic;
using Module.EntitiesInfo;

namespace Web
{
    public partial class xemlive : System.Web.UI.Page
    {
        protected static string userName = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                /*
                userName = MemberBL.GetValue();
                if (userName == null)
                    Response.Redirect("dangnhap.aspx");

                txtMsg.Attributes.Add("onkeypress", "return clickButton(event,'btn')");
                if (!IsPostBack)
                {
                    MemberInfo info = MemberBL.GetInfo(3, userName, "");
                    if (info != null)
                    {


                        hdnRoomID.Value = Request.QueryString["rid"];
                        ChatRoom room = ChatEngine.GetRoom(hdnRoomID.Value);
                        string prevMsgs = room.JoinRoom(info.IdMember.ToString(), userName);
                        txt.Text = prevMsgs;
                        foreach (string s in room.GetRoomUsersNames())
                        {
                            lstMembers.Items.Add(new ListItem(s, s));
                        }
                    }

                }
                 */
            }
            catch (Exception)
            {                                
            }

        }


        #region Script Callback functions

        /// <summary>
        /// This function is called from the client script 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="roomID"></param>
        /// <returns></returns>
        [WebMethod]
        static public string SendMessage(string msg, string roomID)
        {
            try
            {
                ChatRoom room = ChatEngine.GetRoom(roomID);
                string res = "";
                if (room != null)
                {
                    res = room.SendMessage(msg,userName);
                }
                return res;
            }
            catch (Exception ex)
            {

            }
            return "";
        }


        /// <summary>
        /// This function is called peridically called from the user to update the messages
        /// </summary>
        /// <param name="otherUserID"></param>
        /// <returns></returns>
        [WebMethod]
        static public string UpdateUser(string roomID)
        {
            try
            {
                ChatRoom room = ChatEngine.GetRoom(roomID);
                if (room != null)
                {
                    string res = "";
                    if (room != null)
                    {
                        res = room.UpdateUser(userName);
                    }
                    return res;
                }
            }
            catch (Exception ex)
            {

            }
            return "";
        }


        /// <summary>
        /// This function is called from the client when the user is about to leave the room
        /// </summary>
        /// <param name="otherUser"></param>
        /// <returns></returns>
        [WebMethod]
        static public string LeaveRoom(string roomID)
        {
            try
            {
                ChatRoom room = ChatEngine.GetRoom(roomID);
                if (room != null)
                    room.LeaveRoom(userName);
            }
            catch (Exception ex)
            {

            }
            return "";
        }


        /// <summary>
        /// Returns a comma separated string containing the names of the users currently online
        /// </summary>
        /// <param name="roomID"></param>
        /// <returns></returns>
        [WebMethod]
        static public string UpdateRoomMembers(string roomID)
        {
            try
            {
                ChatRoom room = ChatEngine.GetRoom(roomID);
                if (room != null)
                {
                    IEnumerable<string> users = room.GetRoomUsersNames();
                    string res = "";

                    foreach (string s in users)
                    {
                        res += s + ",";
                    }
                    return res;
                }
            }
            catch (Exception ex)
            {
            }
            return "";
        }
        #endregion


       
    }
}